<template>
  <XtxBread>
    <XtxBreadItem to="/">首页</XtxBreadItem>
    <Transition name="fade-right" mode="out-in">
      <XtxBreadItem
      :key="cateInfo.fcate.id"
        v-if="cateInfo.fcate"
        :to="`/category/${cateInfo.fcate.id}`"
        >{{ cateInfo.fcate.name }}</XtxBreadItem
      >
    </Transition>
    <Transition name="fade-right" mode="out-in">
      <XtxBreadItem :key="cateInfo.scate.id" v-if="cateInfo.scate">{{
        cateInfo.scate.name
      }}</XtxBreadItem>
    </Transition>
  </XtxBread>
</template>
<script>
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { computed } from 'vue'
export default {
  name: 'SubBread',
  setup () {
    const store = useStore()
    const route = useRoute()
    const cateInfo = computed(() => {
      const info = {
        fcate: null,
        scate: null
      }
      // 动态计算当前分类的信息:一级分类和二级分类
      store.getters.cates.some((firstCate) => {
        // 查找当前二级分类
        const secondCate =
          firstCate.children &&
          firstCate.children.find((item) => item.id === route.params.id)
        if (secondCate) {
          // 找到了二级分类
          info.fcate = {
            id: firstCate.id,
            name: firstCate.name
          }
          info.scate = {
            id: secondCate.id,
            name: secondCate.name
          }
          // 终止后续遍历
          return true
        }
      })
      return info
    })
    return { cateInfo }
  }
}
</script>
